package cn.edu.scujj.dao;

import cn.edu.scujj.bean.TongJi;
import cn.edu.scujj.utils.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class TongjiDao {

    // 添加商品统计记录
    public boolean addTongji(TongJi tj) {
        String sql = "INSERT INTO tongji (name, num, money, profit, inventory) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DButil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, tj.getName());
            pstmt.setDouble(2, tj.getNum());
            pstmt.setFloat(3, tj.getMoney());
            pstmt.setFloat(4, tj.getProfit());
            pstmt.setDouble(5, tj.getInventory());

            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 更新商品统计记录
    public boolean updateTongji(TongJi tj) {
        String sql = "UPDATE tongji SET num = ?, money = ?, profit = ?, inventory = ? WHERE name = ?";
        try (Connection conn = DButil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setDouble(1, tj.getNum());
            pstmt.setFloat(2, tj.getMoney());
            pstmt.setFloat(3, tj.getProfit());
            pstmt.setDouble(4, tj.getInventory());
            pstmt.setString(5, tj.getName());

            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除商品统计记录
    public boolean deleteTongji(String name) {
        String sql = "DELETE FROM tongji WHERE name = ?";
        try (Connection conn = DButil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, name);

            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 查询所有商品统计记录
    public List<TongJi> findAll() {
        List<TongJi> list = new ArrayList<>();
        String sql = "SELECT name, num, money, profit, inventory FROM tongji";
        try (Connection conn = DButil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                TongJi tj = new TongJi();
                tj.setName(rs.getString("name"));
                tj.setNum(rs.getDouble("num"));
                tj.setMoney(rs.getFloat("money"));
                tj.setProfit(rs.getFloat("profit"));
                tj.setInventory(rs.getDouble("inventory"));
                list.add(tj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    public TongJi[] findAllTongJiByName(String name) {
        Connection conn = DButil.getConnection();
        List<TongJi> list = new ArrayList<>();
        String sql = "SELECT name, num, money, profit, inventory FROM tongji WHERE name = ?";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, name);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                TongJi tj = new TongJi();
                tj.setName(rs.getString("name"));
                tj.setNum(rs.getDouble("num"));
                tj.setMoney(rs.getFloat("money"));
                tj.setProfit(rs.getFloat("profit"));
                tj.setInventory(rs.getDouble("inventory"));
                list.add(tj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list.toArray(new TongJi[0]);
    }
}

